﻿<!DOCTYPE html>
<html>
<head>
    <title>CGI</title>
</head>
<body>
    <h1>Our Page</h1>
    <script>
        //01
        //Object is constructor function
       
        console.log(typeof Object);
        //obj is object is JS
        var obj = new Object();
        console.log(typeof obj);
        

        //02
        //brand new object with "name" parameter set to "JJ"
        /*
        var Employee = function (name) {
            this.name = name;
        };
        var newEmployee = new Employee('JJ');
        //newEmployee is an object ... can think as type Employee with capital E
        console.log(typeof newEmployee);
        console.log(newEmployee.name);
        */

        //03
        //contructor function with multiple arguments
        /*
        var Employee = function (name, boss) {
            this.name = name;
            this.boss = boss;
        };
        var newEmployee = new Employee('JJ', 'JD');
        console.log(newEmployee.boss);
        */

        //04
        //2 seperate instance of Employee -- 2 separate objects
        /*
        var Employee = function (name) {
            this.name = name;
        };
        var e1 = new Employee('JJ');
        var e2 = new Employee('JD');
        console.log(e1 === e2);
        //but both have same prototype, hence we can add common functionality to Employee prototype
        console.log(e1.__proto__ === e2.__proto__);
        */

        //05
        /*
        var Employee = function (name) {
            this.name = name;
            this.giveRaise = function () {
            };
        };
        var e1 = new Employee('JJ');
        var e2 = new Employee('JD');
        //problem here...if we have 100,1000 objects,then this function duplicated 100,1000 times
        //hence don't add functions within constructor function, adds to their prototype
        console.log(e1.giveRaise === e2.giveRaise);
        //when we work with constructor function, we have access to the prototype. JSON object havn't access
        console.log(typeof Employee.prototype);
        //false, JSON object has sets to Object.prototype, with contruction function, Employee has it's own empty prototype
        console.log(Employee.prototype === Object.prototype);
        */

        //06
        //add giveRaise to Employee prototype
        /*
        var Employee = function (name) {
            this.name = name;
        };
        Employee.prototype.giveRaise = function () {
        };
        var e1 = new Employee('JJ');
        var e2 = new Employee('JD');
        console.log(e1.giveRaise === e2.giveRaise);
        */

        //07
        //here this.salary apply only to e1 and not e2
        /*
        var Employee = function (name) {
            this.name = name;
            this.salary = 50000;
        };
        Employee.prototype.giveRaise = function (raise) {
            this.salary += raise;
        };
        var e1 = new Employee('JJ');
        var e2 = new Employee('JD');
        e1.giveRaise(100000);
        console.log(e1.salary);
        console.log(e2.salary);
        */
    </script>
</body>
</html>